import React from "react";
import { ICLoseBusType, IGetCloseBtnVisibleParams } from "./interface";
import { getCloseZhWord } from "./getCloseWord";
import { getCloseBtnVisible } from "./getCloseBtnVisible";

export interface CloseBtnProps
  extends IGetCloseBtnVisibleParams,
    React.DetailedHTMLProps<
      React.AnchorHTMLAttributes<HTMLAnchorElement>,
      HTMLAnchorElement
    > {
  format?: (p: { busType: ICLoseBusType; zhWord: string }) => string;
}

export const CloseBtn = ({
  busType = 1,
  record,
  format,
  ...rest
}: CloseBtnProps) => {
  const zhWord = getCloseZhWord({
    busType,
  });

  const closeBtnVisible = record
    ? getCloseBtnVisible({
        record,
        busType,
      })
    : true;

  if (!closeBtnVisible) {
    return false;
  }
  return (
    <a {...rest}>
      {format
        ? format({
            zhWord,
            busType,
          })
        : zhWord}
    </a>
  );
};
